﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Teenusekiht ;

public partial class UserControls_CommOutControl : System.Web.UI.UserControl
{

    public byte Sets {get;set;}
    private bool IsByEvent = false ;

    private ASP . CommControl [] cocs ;
    private LinkButton [] lbs ;

    private int? kId ;

    private CommServ cs ;
    private GenCom [] result = null ;
    private RatCom [] values = null ;
    private bool hasItems = false ;
    private int pCount = -1 ;
    private int pCurrent = -1 ;
    private int pNew = -1 ;

    protected void Page_Init ( object sender , EventArgs e )
    {
        cocs = new ASP . CommControl [ SETS . DEFNUMCOM_FS ] ;
        for ( int i = 0 ; i < SETS . DEFNUMCOM_FS ; i ++ ) {
            cocs [ i ] = ( ASP . CommControl ) LoadControl ( "~/UserControls/CommControl.ascx" ) ;
            divCenter . Controls . Add ( cocs [ i ] ) ; }
        lbs = new LinkButton [ SETS . DEFNUMPAG_FS ] ;
        for ( int i = 0 ; i < SETS . DEFNUMPAG_FS ; i ++ ) {
            lbs [ i ] = new LinkButton () ;
            divBottom . Controls . Add ( lbs [ i ] ) ; }
    }

    protected void Page_Load ( object sender , EventArgs e ) //NUM COMS VÕIKS OLLA MUUDETAV LEHEKÜLJE TÜÜBIGA!!! -MUUTUS SEADA SIIN VIST...
    {
        cs = new CommServ ( Sets ) ;
        foreach ( LinkButton lb in lbs ) {
            lb . Click += new EventHandler ( LButton_Click ) ; }
        if ( ! IsPostBack ) {
            pCurrent = pNew = 1 ;
            RetrieveData () ;
            ShowData () ; }
    }

    public void SetVals ( int? kId , int? uId , string uNa , string uDi )
    {
        this . kId = kId ;
        foreach ( ASP . CommControl cc in cocs ) {
            cc . Sets = Sets ;
            cc . IsInp = false ;
            cc . SetVals ( kId , uId , uNa , uDi ) ; }
    }

    private void MakeCurrent ()
    {
        HiddenF . Value = "" + pNew ;
    }

    private void RetrieveData ()//muuta...
    {
        int? count = null ;
        if ( pCount < 0 ) count = cs . GetCount ( kId ) ;
        if ( pNew > 0 ) result = cs . GetComms ( kId , pNew , SETS . DEFNUMCOM_FS ) ; //NUM COMS VÕIKS OLLA MUUDETAV LEHEKÜLJE TÜÜBIGA!!!
        if ( result != null ) values = cs . RetrRates () ; //SEELLE SEATUST TULEKS KA KONTROLLIDA!! (kui see on null, peaks ka result'i null panema ja midagi ei tohiks teha)
        if ( count != null ) {
            pCount = ( int ) Math . Ceiling ( ( ( double ) count ) / SETS . DEFNUMCOM_FS ) ;
            if ( count != 0 ) hasItems = true ; }
    }

    private void SetLB ( LinkButton lb , bool stle , string text )
    {
        if ( text == null ) {
            lb . Visible = false ;
            lb . Text = "" ; }
          else {
            lb . Visible = true ;
            lb . Text = text ;
            lb . Font . Size = stle ? FontUnit . XLarge : FontUnit . Medium ;
            lb . Width = Unit . Parse ( "2em" ) ; }
    }

    private void PageData ()//andmete page'imine
    {
        int n = 0 ;
        if ( hasItems ) {
            SetLB ( lbs [ n ] , false , "<" ) ;
            if ( pCount <= ( SETS . DEFNUMPAG_FS - 2 ) ) {
                for ( n ++ ; n <= pCount ; n ++ ) {
                    if ( n == pNew ) SetLB ( lbs [ n ] , true , n . ToString () ) ;
                      else SetLB ( lbs [ n ] , false , n . ToString () ) ; } }
              else {
                n ++ ;
                SetLB ( lbs [ n ] , false , "|<" ) ;
                int pagesShort = ( SETS . DEFNUMPAG_FS - 4 ) / SETS . DEFPAGDIV ;
                int pagesLong = ( ( SETS . DEFNUMPAG_FS - 4 ) - 1 ) - pagesShort ;//-1, et välja jätta praegu valitud
                int left , right ;
                if ( pNew < pCurrent ) { left = pagesLong ; right = pagesShort ; }
                  else { left = pagesShort ; right = pagesLong ; }
                int numAd = -2 ;
                if ( pNew <= left ) numAd += 1 ;
                  else if ( ( pCount - pNew ) <= right ) numAd += pCount - ( ( SETS . DEFNUMPAG_FS - 4 ) - 1 ) ;
                  else numAd += pNew - left ; //tava juht - loendamist alustatakse praegune-vasakul olevast
                for ( n ++ ; n < ( SETS . DEFNUMPAG_FS - 2 ) ; n ++ ) {
                    if ( ( n + numAd ) == pNew ) SetLB ( lbs [ n ] , true , ( n + numAd ) . ToString () ) ;
                      else SetLB ( lbs [ n ] , false , ( n + numAd ) . ToString () ) ; }
                SetLB ( lbs [ n ] , false , ">|" ) ;
                n ++ ; }
            SetLB ( lbs [ n ] , false , ">" ) ; }
          else {
            SetLB ( lbs [ n ] , true , SETS . LIPG_REFRESH_TE ) ; }
        for ( n ++ ; n < SETS . DEFNUMPAG_FS ; n ++ )
            SetLB ( lbs [ n ] , false , null ) ;
    }

    private void ShowData ()//muuta...
    {
        if ( hasItems ) {
            HiddenV . Value = result . Length . ToString () ;
            for ( int i = 0 ; i < SETS . DEFNUMCOM_FS ; i ++ ) {
                if ( result . Length > i )
                    cocs [ i ] . SetFields ( result [ i ] , values ) ;
                  else
                    divCenter . Controls . Remove ( cocs [ i ] ) ; } }
          else {
            HiddenV . Value = "0" ;
            for ( int i = 0 ; i < SETS . DEFNUMCOM_FS ; i ++ ) {
                divCenter . Controls . Remove ( cocs [ i ] ) ; } }
        PageData () ;
        MakeCurrent () ;
    }

    protected void LButton_Click ( object sender , EventArgs e )
    {
        LinkButton lButton = ( LinkButton ) sender ;
        RetrieveData () ;
        int parsed ;
        pCurrent = ( int . TryParse ( HiddenF . Value , out parsed ) ) ? parsed : 1 ;
        if ( pCurrent < 1 ) pCurrent = 1 ;
          else if ( pCurrent > pCount ) pCurrent = pCount ;//count on 1 ka siis kui objekte on 0
        if ( lButton . Text == "<" ) pNew = pCurrent - 1 ;
          else if ( lButton . Text == ">" ) pNew = pCurrent + 1 ;
          else if ( lButton . Text . StartsWith ( "|" ) ) pNew = 1 ;
          else if ( lButton . Text . EndsWith ( "|" ) ) pNew = pCount ;
          else pNew = ( int . TryParse ( lButton . Text , out parsed ) ) ? parsed : 1 ; //ka värskenda teksti korral läheb 1. lehele
        if ( pNew < 1 ) pNew = 1 ;
          else if ( pNew > pCount ) pNew = pCount ;
        if ( hasItems ) RetrieveData () ;
        IsByEvent = true ;
    }

    protected void Page_PreRender ( object sender , EventArgs e )
    {
        if ( IsByEvent ) {
            ShowData () ; }
          else if ( IsPostBack ) {//see ümber teha - vaja andmete võtmist korrastada (kui count null (-1) on viga, kui 0 on tühi, kui üle 0 on lehti)!!
            int parsed ;
            int count = ( int . TryParse ( HiddenV . Value , out parsed ) ) ? parsed : SETS . DEFNUMCOM_FS ;
            if ( ( count < 1 ) || ( count > SETS . DEFNUMCOM_FS ) ) count = SETS . DEFNUMCOM_FS ;
            for ( int i = SETS . DEFNUMCOM_FS - 1 ; i != count - 1 ; i -- ) {
                divCenter . Controls . Remove ( cocs [ i ] ) ; } }
    }

    protected void Page_Unload ( object sender , EventArgs e )
    {
        if ( cs != null ) cs . Dispose () ;
    }

}